package vue;

import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import modele.ModeleAttribut;
import modele.ModeleClasse;
import modele.ModeleMethode;

import controleur.ControleurPanelClasse;
import controleur.ControleurToile;


public class VueAjoutClasse extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JButton btnval;
	private DefaultTableModel modela;
	private DefaultTableModel modelm;
	private JTable tattributs;
	private JTable tmethodes;
	private JPanel contenant;
	private JPanel title;
	private JPanel nc;
	private JPanel jt;
	private JPanel panbtnval;
	private JTextField txtnc;
	private int x;
	private int y;
	private String ancienNom = "";

	private JTabbedPane jtp;
	private JTabbedPane jtp2;

	private int nbMethodes = 0;


	public VueAjoutClasse(ControleurPanelClasse c, ModeleClasse mc) {

		super.setTitle("Modification de Classe");
		
		panbtnval = new JPanel();
		btnval = new JButton("Valider");
		btnval.setActionCommand("ValiderModifier");
		panbtnval.add(btnval);
		btnval.addActionListener(c);
		
		init();
		
		Vector<ModeleAttribut> va = mc.getAttributs();
		Vector<ModeleMethode> vm = mc.getMethodes();
		
		this.txtnc.setText(mc.getNom());
		this.ancienNom = mc.getNom();

		if(!va.isEmpty())
			this.modela.setRowCount(0);

		if(!vm.isEmpty())
			this.modelm.setRowCount(0);

		for (int i = 0; i<va.size(); i++){
			this.modela.addRow(new Object[]{va.elementAt(i).getNom(), va.elementAt(i).getType()});
		}

		for (int i = 0; i<vm.size(); i++){
			this.modelm.addRow(new Object[]{vm.elementAt(i).getNom(), vm.elementAt(i).getTypeRetour()});
			
			if(vm.elementAt(i).getListeArgs().size() > 0) {
				PanMethodes pm = new PanMethodes(vm.elementAt(i).getNom(), nbMethodes);
				pm.setAttributs(vm.elementAt(i).getListeArgs());
				jtp2.addTab(vm.elementAt(i).getNom(), pm);
			}
			nbMethodes++;
		}
		
		afficher();
	}
	
	public VueAjoutClasse(ControleurToile c, int xx, int yy) {
		this.x = xx;
		this.y = yy;
		
		super.setTitle("Creation de Classe");
		
		panbtnval = new JPanel();
		btnval = new JButton("Valider");
		btnval.setActionCommand("ValiderAjout");
		panbtnval.add(btnval);
		btnval.addActionListener(c);
		
		init();		
		afficher();
	}
	
	public void afficher() {
		super.pack();
		super.setVisible(true);		
	}
	
	public void init() {
		String[] valuesAtt = new String[]{"VARCHAR", "INT", "DECIMAL", "DOUBLE", "FLOAT"};
		String[] valuesMeth = new String[]{"VOID", "VARCHAR", "INT", "DECIMAL", "DOUBLE", "FLOAT"};

		JPanel panprinc = new JPanel();

		jtp = new JTabbedPane();
		jtp.addTab("Classe", panprinc);
		
		jtp2 = new JTabbedPane();
		
		jtp.addTab("Methodes", jtp2);
		
		contenant = new JPanel();
		contenant.setLayout(new GridLayout(2,1));
		
		String [] fieldNames = {"Nom de la classe"};

		title = new JPanel();
		title.add(new JLabel("<html> Remplir les informations ci-dessous <br/> " +
				"(appuyez sur la flèche du bas pour valider un attribut ou une méthode) </html>"));

		nc = new JPanel();
		JLabel labnc = new JLabel(fieldNames[0]);
		nc.add(labnc);
		txtnc = new JTextField(20);
		nc.add(txtnc);
		
		contenant.add(title);
		contenant.add(nc);

		jt = new JPanel();
		jt.setLayout(new GridLayout(1,2));
		
		tattributs = new JTable(1,2);
		modela = (DefaultTableModel)tattributs.getModel();
		tattributs.getColumnModel().getColumn(0).setHeaderValue("Nom Attributs");
		tattributs.getColumnModel().getColumn(1).setHeaderValue("Type");
		TableColumn cola = tattributs.getColumnModel().getColumn(1);
		cola.setCellEditor(new MyComboBoxEditor(valuesAtt));
		JScrollPane scroll = new JScrollPane(tattributs);
		jt.add(scroll);

		tmethodes = new JTable(1,2);
		modelm = (DefaultTableModel) tmethodes.getModel();
		tmethodes.getColumnModel().getColumn(0).setHeaderValue("Nom Methodes");
		tmethodes.getColumnModel().getColumn(1).setHeaderValue("Type de retour");
		TableColumn colm = tmethodes.getColumnModel().getColumn(1);
		colm.setCellEditor(new MyComboBoxEditor(valuesMeth));
		JScrollPane scroll2 = new JScrollPane(tmethodes);
		jt.add(scroll2);

		tattributs.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e){	
				if (e.getKeyCode() == e.VK_ENTER){
					modela.addRow(new Object[]{"",""});
				}
				if(e.getKeyCode() == e.VK_DELETE){
					int x = tattributs.getSelectedRow();
					modela.removeRow(x);
				}
			}
		});

		tmethodes.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e){	
				if (e.getKeyCode() == e.VK_ENTER){
					modelm.addRow(new Object[]{"",""});
				}
				else if(e.getKeyCode() == e.VK_DELETE){
					int x = tmethodes.getSelectedRow();
					modelm.removeRow(x);
				}
				else if (e.getKeyCode() == e.VK_RIGHT){
					System.out.println("tmethodes.getSelectedRow() = " + tmethodes.getSelectedRow());
					System.out.println("modelm.getValueAt(0,0) = " + modelm.getValueAt(0, 0).toString());
					
					String nomMethode = modelm.getValueAt(tmethodes.getSelectedRow(), 0).toString();
					boolean existeDeja = false;
					
					for(int i = 0 ; i < jtp2.getComponentCount() && !existeDeja ; i++) {
						PanMethodes p = (PanMethodes) jtp2.getComponent(i);
						if(p.getNb() == tmethodes.getSelectedRow()) {
							p.getTxtnc().setText(nomMethode);
							jtp2.setTitleAt(i, nomMethode);
							jtp.setSelectedIndex(jtp2.indexOfComponent(jtp2));
							jtp2.setSelectedIndex(jtp2.indexOfComponent(p));
							jtp2.validate();
							jtp2.setVisible(true);
		
							existeDeja = true;
						}
					}
					
					if(!existeDeja) {
						PanMethodes pm = new PanMethodes(nomMethode, nbMethodes);
						nbMethodes++;
						jtp2.addTab(nomMethode, pm);	
						jtp.setSelectedIndex(jtp2.indexOfComponent(jtp2));
						jtp2.setSelectedIndex(jtp2.indexOfComponent(pm));
						jtp2.validate();
						jtp2.setVisible(true);
					}
				}
			}
		});
		
		/*super.add("North", contenant);
		super.add("Center", jt);
		super.add("South", panbtnval);*/
		
		panprinc.add("North", contenant);
		panprinc.add("Center", jt);
		panprinc.add("South", panbtnval);
		super.add(jtp);
	}
	
	public PanMethodes getPanMethode(String nom) {
		PanMethodes pm = null;
		
		for(int i = 0 ; i < jtp2.getComponentCount() ; i++) {
			PanMethodes p = (PanMethodes) jtp2.getComponentAt(i);
			if(p.getNom().equals(nom)) {
				return p;
			}
		}
		
		return pm;
	}

	public class PanMethodes extends JPanel{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private int nb;
		private String nom;
		private JTextField txtnc;
		private DefaultTableModel modeltypemeth;
		private JTable typeargsmet;
		

		public PanMethodes(String nomMeth, int nb) {	
			this.nb = nb;
			this.nom = nomMeth;
			
			JPanel pang = new JPanel();
			pang.setLayout(new GridLayout(2,1));

			String [] fieldNames = {"Nom de la methode"};

			pang.add(new JLabel("<html> Vous pouvez modifier les informations ci-dessous <br/> " +
			"(appuyez sur la fleche du bas pour valider un type de retour d'argument d'une methode) </html>"));

			JPanel nc = new JPanel();
			JLabel labnc = new JLabel(fieldNames[0]);
			nc.add(labnc);
			txtnc = new JTextField(nomMeth, 20);
					
			nc.add(txtnc);

			pang.add(nc);
			
			this.add(pang);
			
			typeargsmet = new JTable(1,2);
			modeltypemeth = (DefaultTableModel)typeargsmet.getModel();
			typeargsmet.getColumnModel().getColumn(0).setHeaderValue("Num attributs");
			modeltypemeth.setValueAt(1, 0, 0);
			typeargsmet.getColumnModel().getColumn(1).setHeaderValue("DataType");
			TableColumn col2 = typeargsmet.getColumnModel().getColumn(1);
			String[] values = new String[]{"VARCHAR", "INT", "DECIMAL", "DOUBLE", "FLOAT"};
			col2.setCellEditor(new MyComboBoxEditor(values));
			JScrollPane sp = new JScrollPane(typeargsmet);
			this.add("Center", sp);
			
			typeargsmet.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent e){	
					if (e.getKeyCode() == e.VK_ENTER){
						modeltypemeth.addRow(new Object[]{"",""});
						modeltypemeth.setValueAt(modeltypemeth.getRowCount(), modeltypemeth.getRowCount()-1, 0);
					}
					if(e.getKeyCode() == e.VK_DELETE){
						if(typeargsmet.getRowCount() > 1) {
							int x = typeargsmet.getSelectedRow();
							modeltypemeth.removeRow(x);
							for(int i = 0 ; i < modeltypemeth.getRowCount() ; i++ ) {
								modeltypemeth.setValueAt(i+1, i, 0);
							}
						}
						else if(typeargsmet.getRowCount() == 1) {
							modeltypemeth.setValueAt(1, 0, 0);
							modeltypemeth.setValueAt("", 0, 1);
						}
					}
				}
			});

		}
		
		public void setAttributs(Vector<String> listeArgs) {
			this.modeltypemeth.setRowCount(0);
			for (int i = 0; i<listeArgs.size(); i++){
				this.modeltypemeth.addRow(new Object[]{i+1, listeArgs.get(i)});
			}
		}
		
		public DefaultTableModel getModeltypemeth() {
			return modeltypemeth;
		}

		public void setModeltypemeth(DefaultTableModel modeltypemeth) {
			this.modeltypemeth = modeltypemeth;
		}
		
		public JTextField getTxtnc() {
			return txtnc;
		}

		public void setTxtnc(JTextField txtnc) {
			this.txtnc = txtnc;
		}

		public int getNb() {
			return nb;
		}

		public void setNb(int n) {
			this.nb = n;
		}
	
		public String getNom() {
			return nom;
		}

		public void setNom(String nom) {
			this.nom = nom;
		}
	}
	
	public class MyComboBoxEditor extends DefaultCellEditor {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public MyComboBoxEditor(String[] items) {
			super(new JComboBox(items));
		}
	}

	
	public DefaultTableModel getModela() {
		return modela;
	}

	public void setModela(DefaultTableModel modela) {
		this.modela = modela;
	}

	public DefaultTableModel getModelm() {
		return modelm;
	}

	public void setModelm(DefaultTableModel modelm) {
		this.modelm = modelm;
	}

	public JTable getTattributs() {
		return tattributs;
	}

	public void setTattributs(JTable tattributs) {
		this.tattributs = tattributs;
	}

	public JTable getTmethodes() {
		return tmethodes;
	}

	public void setTmethodes(JTable tmethodes) {
		this.tmethodes = tmethodes;
	}

	public JTextField getTxtnc() {
		return txtnc;
	}

	public void setTxtnc(JTextField txtnc) {
		this.txtnc = txtnc;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
	
	public String getAncienNom() {
		return ancienNom;
	}

	public void setAncienNom(String ancienNom) {
		this.ancienNom = ancienNom;
	}
	
	public JTabbedPane getJtp2() {
		return jtp2;
	}

	public void setJtp2(JTabbedPane jtp2) {
		this.jtp2 = jtp2;
	}
}